import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * 
 * file download window
 *
 */
public class MyBooksGui extends JPanel {

	private static final long serialVersionUID = 1L;
	/** scroll panel for the books */ 
	private JScrollPane jScrollPane = null;
	/** shows books purchased by user */
	private JTable books = null;
	/** Main title label */
	private JLabel titleLabel = null;
	/** get the book button */
	private JButton get = null;
	/** go back to main screen */
	private JButton back = null;
	/** books table column name */
	public String[] ColumnNames={"Serial","Book Name","Language","Date"};
	/** books data (not for display) */
	public String[][] allData=null;
	/** books data to be displayed in the table */
	public String[][] data={};
	/** model for the table */
	public DefaultTableModel model;
	/** window panel */
	public JPanel panel=new JPanel();
	private JButton exit = null;
	

	/**
	 * This is the default constructor
	 */
	public MyBooksGui() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		titleLabel = new JLabel();
		titleLabel.setText("My Books");
		titleLabel.setBounds(new Rectangle(228, 7, 131, 40));
		titleLabel.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,30));
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.gridx = 0;
		panel.setSize(623, 221);
		panel.setLayout(null);
		panel.add(titleLabel, null);
		panel.add(getGet(), null);
		panel.add(getBack(), null);
		allData=Main.myBooksControl.getPurchased();
		if(allData!=null)
		{
			data=new String[allData.length][4];
			for(int i=0;i<allData.length;i++)
				for(int j=0;j<4;j++)
					data[i][j]=allData[i][j];
		}
		panel.add(getJScrollPane(),null);
		panel.setVisible(false);
		panel.setBounds(new Rectangle(379, 106, 13, 13));
		panel.add(getExit(), null);
	}

	/**
	 * This method initializes jScrollPane for books
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(12, 47, 464, 149));
			jScrollPane.setViewportView(getBooks());
			
		}
		return jScrollPane;
	}

	/**
	 * This method initializes books	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getBooks() {
		if (books == null) 
		{
			if(data!=null)
			{
				model=new DefaultTableModel(data,ColumnNames);
				books = new JTable(model);
				TableColumn column=books.getColumnModel().getColumn(0);
				column.setPreferredWidth(50);
				column=books.getColumnModel().getColumn(1);
				column.setPreferredWidth(280);
				column=books.getColumnModel().getColumn(2);
				column.setPreferredWidth(50);
				column=books.getColumnModel().getColumn(3);
				column.setPreferredWidth(50);
			}
			else
			{
				model=new DefaultTableModel(data,ColumnNames);
				books = new JTable(model);
			}	
		}
		return books;
	}

	/**
	 * This method initializes get and adds listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getGet() {
		if (get == null) {
			get = new JButton("Get Book");
			get.setBounds(new Rectangle(500, 57, 85, 27));
			get.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					int row=books.getSelectedRow();
					if(row!=-1)
						Main.myBooksControl.getFile(allData[row][0],allData[row][2],allData[row][4],allData[row][5],allData[row][6],allData[row][1]);
					else
						JOptionPane.showMessageDialog(null,"Choose a Book","ERROR",JOptionPane.ERROR_MESSAGE);
				}
			});
		}
		return get;
	}

	/**
	 * This method initializes back	and adds listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBack() {
		if (back == null) {
			back = new JButton("Back");
			back.setBounds(new Rectangle(500, 96, 83, 27));
			back.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					 
					panel.setEnabled(false);
					panel.setVisible(false);
					if(Main.gusers.authorozations==1) Main.log.frame.setSize(450,320);
					else if(Main.gusers.authorozations==2) Main.log.frame.setSize(450,400);
					else if(Main.gusers.authorozations==3) Main.log.frame.setSize(450,515);
					Main.log.frame.add(Main.welcomeScreen.panel);
					Main.welcomeScreen.panel.setVisible(true);
					Main.welcomeScreen.panel.setEnabled(true);
				}
			});
		}
		return back;
	}

	/**
	 * This method initializes exit	and adds listener
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit() {
		if (exit == null) {
			exit = new JButton("Logout");
			exit.setBounds(new Rectangle(503, 14, 80, 28));
			exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.myBooksControl.exit();
				}
			});
		}
		return exit;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
